﻿using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;
using DotRas;
using System.Threading;
using System.Net;
using System.Collections.ObjectModel;
using System.Linq;

namespace Intorise.Net
{
    public class AdslHelper
    {
        /// <summary>
        /// 创建或更新一个PPPOE连接(指定PPPOE名称)
        /// </summary>
        public static void CreateOrUpdatePPPOE(string updatePPPOEname)
        {
            RasDialer dialer = new RasDialer();
            RasPhoneBook allUsersPhoneBook = new RasPhoneBook();
            string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
            allUsersPhoneBook.Open(path);
            // 如果已经该名称的PPPOE已经存在，则更新这个PPPOE服务器地址
            if (allUsersPhoneBook.Entries.Contains(updatePPPOEname))
            {
                allUsersPhoneBook.Entries[updatePPPOEname].PhoneNumber = " ";
                // 不管当前PPPOE是否连接，服务器地址的更新总能成功，如果正在连接，则需要PPPOE重启后才能起作用
                allUsersPhoneBook.Entries[updatePPPOEname].Update();
            }
            // 创建一个新PPPOE
            else
            {
                string adds = string.Empty;
                ReadOnlyCollection<RasDevice> readOnlyCollection = RasDevice.GetDevices();
                //                foreach (var col in readOnlyCollection)
                //                {
                //                    adds += col.Name + ":" + col.DeviceType.ToString() + "|||";
                //                }
                //                _log.Info("Devices are : " + adds);
                // Find the device that will be used to dial the connection.
                RasDevice device = RasDevice.GetDevices().Where(o => o.DeviceType == RasDeviceType.PPPoE).First();
                RasEntry entry = RasEntry.CreateBroadbandEntry(updatePPPOEname, device);    //建立宽带连接Entry
                entry.PhoneNumber = " ";
                allUsersPhoneBook.Entries.Add(entry);
            }
        }

        public static void Connect(string Connection, string username = "", string password = "")
        {
            try
            {
                CreateOrUpdatePPPOE(Connection);
                RasDialer dialer = new RasDialer();
                dialer.EntryName = Connection;
                dialer.PhoneNumber = " ";
                dialer.AllowUseStoredCredentials = true;
                dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                dialer.Credentials = new NetworkCredential(username, password);
                dialer.Timeout = 1000;
                RasHandle myras = dialer.Dial();
                while (myras.IsInvalid)
                {
                    Thread.Sleep(1000);
                    myras = dialer.Dial();
                }
                if (!myras.IsInvalid)
                {
                    Console.WriteLine("RasDialer Success! " + Convert.ToString(DateTime.Now));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("RasDialer error! " + Convert.ToString(DateTime.Now) + Connection + " error is :: " + ex.ToString());
            }
        }
    }
}